package cn.gok.day7;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @ClassName Damage
 * @Description TODO
 * @Author HP
 * @Date 15:42 2022/7/12
 * @Version 1.0
 **/
public class Damage {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        //    破坏封装性
        Class t=Tmp.class;
        Tmp tmp=new Tmp("oscar",22);
        Method m =t.getDeclaredMethod("fun1",String.class);
        System.out.println("public方法调用");
        m.invoke(tmp,"罗涛");
        Method m2 =t.getDeclaredMethod("fun2");
        System.out.println("private方法调用");
        try {
            m2.invoke(tmp);
        }catch (IllegalAccessException e){
            System.out.println("权限不够无法调用，正在破坏封装性...");
        }finally {
            m2.setAccessible(true);
            m2.invoke(tmp);
        }

    }


}
